package com.wjq.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjq.boot.entity.TAdmin;
import com.wjq.boot.mapper.TAdminMapper;
import com.wjq.boot.service.TAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjq.boot.vo.ResultVo;
import org.hibernate.validator.internal.constraintvalidators.bv.time.futureorpresent.FutureOrPresentValidatorForReadableInstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangjiaquan
 * @since 2024-08-02
 */
@Service
public class TAdminServiceImpl extends ServiceImpl<TAdminMapper, TAdmin> implements TAdminService {

    @Resource
    private TAdminMapper adminMapper;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public Boolean check(String column, String value,Integer id) {
        QueryWrapper<TAdmin> queryWrapper = new QueryWrapper<>();
        long count;
        if(id != null){
            count = count(queryWrapper.eq(column, value).ne("id",id));
        } else {
            count = count(queryWrapper.eq(column, value));
        }
        if(count == 0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean fixState(Long id, Boolean state) {
        UpdateWrapper<TAdmin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        if(state == false){
            updateWrapper.set("state",1);
        } else {
            updateWrapper.set("state",0);
        }
        if(adminMapper.update(null,updateWrapper)>0){
            // 更新成功
            return true;
        } else {
            // 更新失败
            return false;
        }
    }

    @Override
    public TAdmin getById(Long id) {
        // 调用父类写中已经存在的方法即可，如果不这个函数名不是这个即不重名的话，可以用this关键字
        return super.getById(id);
    }

    @Override
    public Boolean add(String name,
                       String nickname,
                       Boolean sex,
                       LocalDate birthday,
                       String phone,
                       String email,
                       String password,
                       Integer state) {
        return this.save(new TAdmin(name,nickname,sex,birthday,phone,email,passwordEncoder.encode(password),state));
    }

    @Override
    public Boolean update(Integer id, String name, String nickname, Boolean sex, LocalDate birthday, String phone, String email, Integer state) {
        // 创建UpdateWrapper实例
        UpdateWrapper<TAdmin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        if (name != null) {
            updateWrapper.set("name", name);
        }
        if (nickname != null) {
            updateWrapper.set("nickname", nickname);
        }
        if (sex != null) {
            updateWrapper.set("sex", sex);
        }
        if (birthday != null) {
            updateWrapper.set("birthday", birthday);
        }
        if (phone != null) {
            updateWrapper.set("phone", phone);
        }
        if (email != null) {
            updateWrapper.set("email", email);
        }
        if (state != null) {
            updateWrapper.set("state", state);
        }
        return (adminMapper.update(null, updateWrapper) > 0);
    }

    @Override
    public Page<TAdmin> getTargetList(Integer pageNo, Integer pageSize, String keyword) {
        // 创建分页对象
        Page<TAdmin> page = new Page<>(pageNo,pageSize);
        // 创建条件构造器
        QueryWrapper<TAdmin> queryWrapper = new QueryWrapper<>();
        // 添加查询条件
        queryWrapper.like("name",keyword);
        return adminMapper.selectPage(page,queryWrapper);
    }


}
